package com.kongjs.application.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjs.application.convert.TenantsConverter;
import com.kongjs.application.mapper.TenantsMapper;
import com.kongjs.application.model.dto.*;
import com.kongjs.application.model.entity.Tenants;
import com.kongjs.application.service.ITenantService;
import com.kongjs.application.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Transactional
@Service
public class TenantServiceImpl extends ServiceImpl<TenantsMapper, Tenants> implements ITenantService {

    @Resource
    private TenantsConverter tenantsConverter;

    @Override
    public boolean save(SaveTenantDTO dto) {
        Tenants tenants = tenantsConverter.toEntity(dto);
        return save(tenants);
    }

    @Override
    public boolean remove(Long id) {
        return removeById(id);
    }

    @Override
    public boolean update(UpdateTenantDTO dto) {
        Tenants tenants = tenantsConverter.toEntity(dto);
        return updateById(tenants);
    }

    @Override
    public QueryTenantDTO query(Long id) {
        Tenants tenants = getById(id);
        return tenantsConverter.toDto(tenants);
    }

    @Override
    public List<QueryTenantDTO> queryList() {
        List<Tenants> tenants = lambdaQuery().eq(Tenants::getEnabled, 1).list();
        return tenantsConverter.toDtos(tenants);
    }

    @Override
    public PageDTO<QueryTenantDTO> page(PageQueryDTO dto, QueryTenantParamDTO paramDTO) {
        Page<Tenants> page;
        if (!ObjectUtils.isEmpty(paramDTO)) {
            page = lambdaQuery()
                    .eq(!ObjectUtils.isEmpty(paramDTO.getId()), Tenants::getId, paramDTO.getId())
                    .like(StringUtils.hasText(paramDTO.getTenantName()), Tenants::getTenantName, paramDTO.getTenantName())
                    .like(StringUtils.hasText(paramDTO.getRemark()), Tenants::getRemark, paramDTO.getRemark())
                    .eq(!ObjectUtils.isEmpty(paramDTO.getEnabled()), Tenants::getEnabled, paramDTO.getEnabled())
                    .between(!ObjectUtils.isEmpty(paramDTO.getStartDate()) && !ObjectUtils.isEmpty(paramDTO.getEndDate()), Tenants::getCreateDate, paramDTO.getStartDate(), paramDTO.getEndDate())
                    .orderByDesc(Tenants::getUpdateDate)
                    .page(Page.of(dto.getPageNum(), dto.getPageSize()));
        } else {
            page = lambdaQuery()
                    .orderByDesc(Tenants::getUpdateDate)
                    .page(Page.of(dto.getPageNum(), dto.getPageSize()));
        }
        IPage<QueryTenantDTO> iPage = page.convert(tenants -> tenantsConverter.toDto(tenants));
        return PageUtil.pageDto(iPage);
    }
}
